
package com.snail.mobile.sim.oss.userInfo.dao.hibernate;

import java.util.Collection;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.springframework.orm.ObjectRetrievalFailureException;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.sungrid.seine.oss.framework.dao.hibernate.BaseDaoHibernate;
import com.snail.mobile.sim.oss.userInfo.model.UserInfo;
import com.snail.mobile.sim.oss.userInfo.dao.UserInfoDao;

public class UserInfoDaoHibernate extends BaseDaoHibernate implements UserInfoDao 
{
    /**
     * @see com.snail.mobile.sim.oss.userInfo.dao.UserInfoDao#getUserInfo(Integer nid)
     */
    public UserInfo getUserInfo(final Integer nid) 
    {
        UserInfo userInfo = (UserInfo) getHibernateTemplate().get(UserInfo.class, nid);
        if (userInfo == null) 
        {
            log.warn("uh oh, userInfo with nid '" + nid + "' not found...");
            throw new ObjectRetrievalFailureException(UserInfo.class, nid);
        }
        return userInfo;
    }

	public UserInfo getUserInfoSimple(Integer nid) {
		return(UserInfo) getHibernateTemplate().get(UserInfo.class, nid);
	}

    /**
     * @see com.snail.mobile.sim.oss.userInfo.dao.UserInfoDao#saveUserInfo(UserInfo userInfo)
     */    
    public void saveUserInfo(final UserInfo userInfo) 
    {
        getHibernateTemplate().save(userInfo);
    }

    /**
     * @see com.snail.mobile.sim.oss.userInfo.dao.UserInfoDao#saveOrUpdateUserInfo(UserInfo userInfo)
     */    
    public void saveOrUpdateUserInfo(final UserInfo userInfo) 
    {
        getHibernateTemplate().saveOrUpdate(userInfo);
    }

    /**
     * @see com.snail.mobile.sim.oss.userInfo.dao.UserInfoDao#removeUserInfo(Integer nid)
     */
    public void removeUserInfo(final Integer nid) 
    {
        getHibernateTemplate().delete(getUserInfo(nid));
    }

    /**
     * @see com.snail.mobile.sim.oss.userInfo.dao.UserInfoDao#getUserInfos(com.snail.mobile.sim.oss.userInfo.model.UserInfo)
     */
    public List getUserInfos(final UserInfo userInfo) 
    {
       //  return getHibernateTemplate().find("from UserInfo order by nid desc");

        if (userInfo == null) 
        {
            return getHibernateTemplate().find("from UserInfo");
        } else 
        {
            // filter on properties set in the userInfo
            HibernateCallback callback = new HibernateCallback() 
            {
                public Object doInHibernate(Session session) throws HibernateException 
                {
                    Example ex = Example.create(userInfo).ignoreCase().enableLike(MatchMode.ANYWHERE);
                    return session.createCriteria(UserInfo.class).add(ex).addOrder(Order.desc("nid")).list();
                }
            };
            return (List) getHibernateTemplate().execute(callback);
        }
    }

    /**
     * @see com.snail.mobile.sim.oss.userInfo.dao.UserInfoDao#removeUserInfos(Collection userInfos)
     */
    public void removeUserInfos(final Collection userInfos)
    {
        getHibernateTemplate().deleteAll(userInfos);
    }

    /**
     * @see com.snail.mobile.sim.oss.userInfo.dao.UserInfoDao#saveUserInfos(Collection userInfos)
     */
    public void saveUserInfos(final Collection userInfos)
    {
        getHibernateTemplate().saveOrUpdateAll(userInfos);
    }
}
